package lc20240502;

//383.赎金信

import java.util.HashMap;

public class Demo1 {

    public static void main(String[] args) {
        String r="aa";
        String m="aab";
        System.out.println("canConstruct(r,m) = " + canConstruct(r, m));
    }
    public static boolean canConstruct(String ransomNote, String magazine) {
        HashMap<Character,Integer> map=new HashMap<>();

        for (char c:magazine.toCharArray()) {
            //使用getOrDefault避免空指针异常
            map.put(c,map.getOrDefault(c,0)+1);
        }
        for (int i = 0; i < ransomNote.length(); i++) {
            char temp=ransomNote.charAt(i);
            //加入map.get(temp)>0。
            if (map.containsKey(temp)&&map.get(temp)>0){
                map.put(temp,map.get(temp)-1);
            }else {
                return false;
            }
        }
        return true;
    }
}
